/*
 * Copyright (C) 2012 Rothens
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package rpg.gfx;

/**
 * rothens.tarhely.biz
 *
 * @author Rothens
 */
public class Bitmap {

    public final int width;
    public final int height;
    public final int[] pixels;

    public Bitmap(int width, int height) {
        this.width = width;
        this.height = height;
        pixels = new int[width * height];
        for (int i = 0; i < pixels.length; i++) {
            pixels[i] = 0xff00ff;
        }
    }

    public Bitmap draw(Bitmap bitmap, int xOffs, int yOffs) {
        for (int y = 0; y < bitmap.height; y++) {
            int yPix = y + yOffs;
            if (yPix < 0 || yPix >= height) {
                continue;
            }

            for (int x = 0; x < bitmap.width; x++) {
                int xPix = x + xOffs;
                if (xPix < 0 || xPix >= width) {
                    continue;
                }
                int src = bitmap.pixels[x + y * bitmap.width];
                if (src != 0xff00ff && src != -65281) {
                    pixels[xPix + yPix * width] = src;
                }
            }
        }
	return this;
    }
    
     public void drawOver(Bitmap bitmap, int xOffs, int yOffs) {
        for (int y = 0; y < bitmap.height; y++) {
            int yPix = y + yOffs;
            if (yPix < 0 || yPix >= height) {
                continue;
            }

            for (int x = 0; x < bitmap.width; x++) {
                int xPix = x + xOffs;
                if (xPix < 0 || xPix >= width) {
                    continue;
                }
                int src = bitmap.pixels[x + y * bitmap.width];
                if (src != 0xff00ff && src != -65281) {
                    pixels[xPix + yPix * width] = (pixels[xPix + yPix * width] | src);
                }
                
            }
        }
    }
     
     public int addColors(int a, int b){

         return 0;
     }

    /**
     * Rotates a Bitmap to the left. (Counter-clockwise)
     */
    public Bitmap rotate() {
        Bitmap ret = new Bitmap(height, width);
        for (int i = 0; i < height * width; i++) {
            int col = i % width;
            int row = i / width;
            ret.pixels[((width - col) * height) - ((height - row))] = pixels[i];
        }
        return ret;
    }

    public void clear() {
        for (int i = 0; i < height * width; i++) {
            pixels[i] = 0xff00ff;
        }
    }
    
    public void clearWith(int c){
	    for(int i = 0; i < height * width; i++) {
		    pixels[i] = c;
	    }
    }
    
    public Bitmap simpleRecolor(int from, int to){
	    Bitmap ret = new Bitmap(width, height);
	    for(int i = 0; i < pixels.length; i++){
		    if(pixels[i] == from){
			    ret.pixels[i] = to;
		    } else {
			    ret.pixels[i] = pixels[i];
		    }
	    }
	    return ret;
    }
}
